package com.eyas.business.service.coachinfo;

import com.eyas.business.config.webmvc.interceptor.LoginInterceptor;
import com.eyas.business.model.jdbcEntity.table.CoachInfo;
import com.eyas.business.model.jdbcEntity.table.RecordAndCoach;
import com.eyas.business.model.jpa.AdminInfo;
import com.eyas.business.model.pojo.coachinfo.AssistantDTO;
import com.eyas.business.model.pojo.coachinfo.QueryParamDTO;
import com.eyas.business.model.pojo.coachinfo.QueryResultDTO;
import com.eyas.business.model.pojo.formselects.FormSelectsResult;
import com.eyas.core.model.page.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;

import static com.eyas.core.jdbc.utils.CommonJdbcUtils.commonJdbcUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 王龙龙
 * @Date: 2019/3/7 17:47
 * @Description:
 */
@Service
@Transactional
public class CoachInfoServiceImpl implements CoachInfoService {
    @Override
    public PageResult<QueryResultDTO> queryCoachInfoForPage(QueryParamDTO dto, int page, int limit) {
        StringBuffer sb = new StringBuffer("");
        List args = new ArrayList();
        sb.append(" select a.*,b.keyworlds as coachleveltext,c.username as addusername ")
                .append(" from coachinfo a ")
                .append(" left join keyworldsinfo b on a.coachlevel=b.keycode and b.dcode='COACHLV' and b.validflag='1' ")
                .append(" left join admininfo c on a.addid=c.adminnid ")
                .append(" where 1=1 ");
        if(StringUtils.hasText(dto.getCoachname())) {
            sb.append(" and a.coachname like ? ");
            args.add("%"+dto.getCoachname()+"%");
        }
        if(StringUtils.hasText(dto.getCoachlevel())) {
            sb.append(" and a.coachlevel=? ");
            args.add(dto.getCoachlevel());
        }
        if(dto.getStarttime()!=null) {
            sb.append(" and a.addtime>=? ");
            args.add(dto.getStarttime());
        }
        if(dto.getEndtime()!=null) {
            sb.append(" and a.addtime<=? ");
            args.add(dto.getEndtime());
        }
        if(StringUtils.hasText(dto.getSex())) {
            sb.append(" and a.sex=? ");
            args.add(dto.getSex());
        }
        if(StringUtils.hasText(dto.getValidflag())) {
            sb.append(" and a.validflag=? ");
            args.add(dto.getValidflag());
        }
        PageResult<QueryResultDTO> pageResult = commonJdbcUtils.queryForPage(sb.toString(),QueryResultDTO.class,page,limit,args);
        return pageResult;
    }

    @Override
    public void changeValidflag(int coachid) {
        CoachInfo coachInfo = commonJdbcUtils.queryObject("select * from coachinfo where coachid=?",CoachInfo.class,coachid);
        if(coachInfo.getValidflag().equals("1")) {
            coachInfo.setValidflag("0");
        }else {
            coachInfo.setValidflag("1");
        }
        commonJdbcUtils.update(coachInfo);
    }

    @Override
    public QueryResultDTO getCoachInfoById(Integer coachid) {
        QueryResultDTO coachInfo = commonJdbcUtils.queryObject("select * from coachinfo where coachid=?",QueryResultDTO.class,coachid);
        return coachInfo;
    }

    @Override
    public void saveCoachInfo(QueryParamDTO dto, HttpSession session) {
        AdminInfo adminInfo = (AdminInfo)session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME);
        if(dto.getCoachid()!=null&&dto.getCoachid()>0) {//update
            CoachInfo coachInfo = commonJdbcUtils.queryObject("select * from coachinfo where coachid=?",CoachInfo.class,dto.getCoachid());
            BeanUtils.copyProperties(dto,coachInfo,"coachid","validflag","addtime","addid","sex");
            if(StringUtils.hasText(dto.getSex())) {
                coachInfo.setSex(dto.getSex());
            }
            commonJdbcUtils.update(coachInfo);
        }else {//insert
            CoachInfo coachInfo = new CoachInfo();
            BeanUtils.copyProperties(dto,coachInfo,"coachid","validflag","addtime","addid","sex");
            coachInfo.setValidflag("1");
            if(StringUtils.hasText(dto.getSex())) {
                coachInfo.setSex(dto.getSex());
            }else {
                coachInfo.setSex("1");
            }
            coachInfo.setAddid(adminInfo.getAdminnid());
            coachInfo.setAddtime(new Date());
            commonJdbcUtils.insert(coachInfo);
        }
    }

    @Override
    public List<CoachInfo> queryCoachInfoList() {
        return commonJdbcUtils.queryList("select coachid,coachname from coachinfo where validflag='1'",CoachInfo.class);
    }

    @Override
    public FormSelectsResult queryAssistantList(Integer recordid) {
        if(recordid==null)
            recordid = -1;
        int coachid = -1;
        String assistants = "";
        List<RecordAndCoach> recordAndCoaches = commonJdbcUtils.queryList("select * from recordandcoach where recordid=? ",RecordAndCoach.class,recordid);
        if(recordAndCoaches!=null&&recordAndCoaches.size()>0) {
            for(RecordAndCoach recordAndCoach:recordAndCoaches) {
                if(recordAndCoach.getCoachflag().equals("1"))
                    coachid = recordAndCoach.getCoachid();
                else
                    assistants += recordAndCoach.getCoachid()+",";
            }
            if(StringUtils.hasText(assistants))
                assistants = assistants.substring(0,assistants.length()-1);
        }

        List<AssistantDTO> list = commonJdbcUtils.queryList("select coachid as value,coachname as name,case when coachid=? then 'disabled' else '' end as disabled ," +
                " case when coachid in ("+(assistants.equals("")?null:assistants)+") then 'selected' else '' end as selected " +
                " from coachinfo where validflag='1' order by coachid ", AssistantDTO.class,coachid);
        FormSelectsResult formSelectsResult = new FormSelectsResult();
        formSelectsResult.setData(list);
        return formSelectsResult;
    }
}
